<?php

namespace App\Containers\AppSection\Ban\UI\API\Transformers;

use App\Containers\AppSection\Ban\Models\Ban;
use App\Containers\AppSection\Profile\UI\API\Transformers\ProfileTransformer;
use App\Containers\AppSection\User\UI\API\Transformers\UserTransformer;
use App\Ship\Parents\Transformers\Transformer;
use League\Fractal\Resource\Item;

class BanTransformer extends Transformer
{
    protected array $availableIncludes = [
        'user', 'profile', 'bannedBy', 'bannable'
    ];

    public function transform(Ban $t): array
    {
        return [
            'object' => $t->getResourceKey(),
            'include' => $this->getAvailableIncludes(),
            'id' => $t->getHashedKey(),
            'ban_comment' => $t->ban_comment,
            'unban_comment' => $t->unban_comment,
            'bannable_type' => !empty($t->user) ? 'user' : 'profile',
            'banned_at' => $this->dateTimeFormatter($t->banned_at),
            'unbanned_at' => $this->dateTimeFormatter($t->unbanned_at),
        ];
    }

    public function includeBannedBy(Ban $t): ?Item
    {
        return $this->nullableItem($t->bannedBy, new ProfileTransformer);
    }

    public function includeBannable(Ban $t): ?Item
    {
        return !empty($t->user) ?
            $this->includeUser($t) :
            $this->includeProfile($t);
    }

    public function includeUser(Ban $t): ?Item
    {
        return $this->nullableItem($t->user, new UserTransformer);
    }

    public function includeProfile(Ban $t): ?Item
    {
        return $this->nullableItem($t->profile, new ProfileTransformer);
    }
}
